<?php

class UserinfoController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    private $_params;
    private $_memcache;
    private $_my_mem;
    public function init(){
        $acl                = new My_Acl();
        $this->_acl         = $acl;
        $auth               = $acl->checkAllowed($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
        $this->_params      = $this->_request->getParams();
        
        $memcache = new Memcache;
        $memcache->connect('localhost', MEMCACHE_PORT) or die ("Could not connect");
        $this->_memcache = $memcache;
        $_my_mem = new My_Mem();
        $this->_my_mem = $_my_mem;
    }
    public function indexAction(){
        # get time start and time end of statistic
        $_params            = $this->_params;
        $gameId             = $_params['game'];
        $this->view->gameId = $gameId;
        $modelGame          = new Default_Model_Game();
        $gameCheck          = $modelGame->getDataById($gameId)->toArray();
        if($gameCheck){
            $this->view->gameCheck = $gameCheck;
            if(!file_exists(LIBRARY_PATH."/My/API/$gameId/Users.php")){
                $this->view->mess = "Không thể tra cứu thông tin user của {$gameCheck['gameName']}!";
                $this->view->flag = 'alert';
                return;
            }
            $usersApi = "My_API_{$gameId}_Users";
            $usersApi = new $usersApi($gameId); 
            if(file_exists(CONFIG_PATH2 . '/'.$gameId.'.ini')){
                #config ssgroup and config game
                $configGame2 = new Zend_Config_Ini(CONFIG_PATH2 . '/'.$gameId.'.ini', APPLICATION_ENV);
                if(!isset($configGame2->getUsernameInfo)){
                    $this->view->mess = 'Không thể tra cứu thông tin nhân vật của game '.$gameCheck['gameName'].'!';
                    $this->view->flag = 'alert';
                    return;
                }
            }
            //get server List       
            if(file_exists(CONFIG_PATH . '/'.$gameId.'.ini')){
                #config ssgroup and config game
                $configGame = new Zend_Config_Ini(CONFIG_PATH . '/'.$gameId.'.ini', APPLICATION_ENV);
               
                $modelRole          = new Default_Model_Role();
                $roleList           = $modelRole->getDataByUserIdGameId($this->_auth['userId'],$gameId)->toArray();
                $channelDisabled    = array();
                foreach($roleList as $row){
                    if(isset($row['channelDisabled']) && $row['channelDisabled']){
                        if(is_array(json_decode($row['channelDisabled']))){
                            $channelDisabled    = array_merge($channelDisabled,json_decode($row['channelDisabled']));
                        }
                    }
                }
                $arrGameId=array('ss31', 'ss32');
                if(!in_array($gameId, $arrGameId))
                {
                    if(isset($configGame->channel)){
                        $channelGame = array();
                        foreach($configGame->channel->toArray() as $key=>$value){
                            $channelGame[] = $key;
                        }
                    }
                    if(isset($configGame->channel)){
                        $arrChannel       = $configGame->channel->toArray();
                    }
                    else{
                        $this->view->mess = 'Không tìm thấy channel';
                        $this->view->flag = 'alert';
                        return;
                    }
                    foreach($arrChannel as $key=>$row){
                        if(!isset($channelDisabled[0]) || (isset($channelDisabled[0]) && !in_array($key,$channelDisabled))){ 
                        }else{
                            unset($arrChannel[$key]);
                            //xoa channel bi disable
                        }
                    }
                    $this->view->arrChannel = $arrChannel;
                  //list channel
                    if(isset($configGame->server)){
                        $arrServer = $configGame->server->toArray();
                        $temp = array();
                        foreach($arrServer as $key=>$server){
                            if(isset($arrTemp) && $arrTemp){
                                if(isset($server['name']) && in_array($key,$arrTemp)){
                                    $temp[$key] = $server['name'];
                                }
                            }else{
                                if(isset($server['name'])){
                                    $temp[$key] = $server['name'];
                                }
                            }
                        }
                        $arrServer = $temp;
    
                        $this->view->arrServer = $arrServer;
                        //list server
                    }
                }//enh kiem tra game ss31
            }
            if(file_exists(CONFIG_PATH2 . '/'.$gameId.'.ini')){
                $configGame = new Zend_Config_Ini(CONFIG_PATH2 . '/'.$gameId.'.ini', APPLICATION_ENV);
                if(isset($_params['bntSubmit']) && $_params['bntSubmit'] == 'Tìm'){
                    $username = '';
                    $channel = '';
                    $serverId = '';
                    if(!isset($_params['username']) || !$_params['username']){
                        $this->view->mess = 'Vui lòng nhập username!';
                        $this->view->flag = 'alert';
                        return;
                    }
                    $username               = trim($_params['username']);
                    $this->view->username   = $username;
                    $getProfile = new My_API_getProfile();
                    if(in_array($gameId, $arrGameId)){
                         $dataRole = $usersApi->getUsernameByRolename($username,$gameId);
                       
                         if(isset($configGame->getUsernameInfo->playgameBy) && $configGame->getUsernameInfo->playgameBy == 'userId'){
                          
                            $username=$dataRole['username'];
                         }
                         if(isset($dataRole) && $dataRole){
                      
                            $dataUser=$getProfile->getUserName(array('userList'=>$username, 'gameId'=>$gameId));
                            if($dataUser){
                                if(isset($configGame->game->value) || isset($configGame->statistics->value)){
                                $arrGame = array();
                                $arrGame1=$configGame->server->toArray();
                                foreach($arrGame1 as $keyGame=>$valGame)
                                {
                                    //$comma_separated = implode("", $valGame);
                                    $arrGame[$keyGame]=implode("", $valGame);
                                }
                                $title = $configGame->statistics->value->toArray();
                                $this->view->title=$title;
                                $this->view->listGame=$arrGame;
                                $this->view->dataRole = $dataRole;
                                $this->view->dataUser=$dataUser;
                                }
                                else{
                                    $this->view->mess = 'Vui lòng kiểm tra lại setting của game!';
                                    $this->view->flag = 'alert';
                                    return;
                                }
                            }
                            else{
                                $this->view->mess = 'Không tìm thấy user '.$username.'!';
                                $this->view->flag = 'alert';
                                return;
                            }
                         }
                         else{
                            $this->view->mess = 'Không tìm thấy user '.$username.'!';
                            $this->view->flag = 'alert';
                            return;
                         } 
                    }//end if kiem tra ss31
                    else{
                    if(isset($_params['channel']) || $_params['channel']){
                        $channel               = trim($_params['channel']);
                        $this->view->channel = $channel;
                    }
                    if(isset($_params['serverId']) && $_params['serverId']){
                        $serverId               = $_params['serverId'];
                        if($arrServer && !isset($arrServer[$serverId])){
                            $this->view->mess = 'Không có dữ liệu!';
                            $this->view->flag = 'alert';
                            return;
                        }
                        $this->view->serverId   = $serverId;
                    }                  
                    $dataUser = array();
                   
                    if(!$serverId){
                        //neu k co serverid, tuc nguoi dung chon tat ca
                        foreach($arrServer as $keyServer=>$row){
                            $result = $usersApi->getUsernameByRolename(array($username),$keyServer);
                            if($result){
                                if(isset($dataUser[$keyServer])){
                                    $dataUser[$keyServer] = array_merge($dataUser[$keyServer],$result);
                                }else{
                                    $dataUser[$keyServer] = $result;
                                }
                            }
                        }
                        //ket thuc for lap server
                    }else{//neu nguoi dung co chon server
                        $result = $usersApi->getUsernameByRolename(array($username),$serverId);
                        if($result){
                            if(isset($dataUser[$serverId])){
                                $dataUser[$serverId] = array_merge($dataUser[$serverId],$result);
                            }else{
                                $dataUser[$serverId] = $result;
                            }
                        }
                    } 
                  
                    $arrUser = array();
                    foreach($dataUser as $row){
                        foreach($row as $user){
                           // if(!strrpos($user['username'],'@') ||
//                                ((strrpos($user['username'],'@') && (!(strrpos($user['username'],'@') || $configGame->getUsernameInfo->playgameBy != 'userId') )))
//                            )
//                            {
                                $arrUser[] = $user['username'];
                            //}        
                        }
                    }  
                    $arrUser = implode(',',$arrUser);
                   // pr($arrUser);
                    if(!$dataUser){
                        $this->view->mess = 'Không tìm thấy username của '.$username.'!';
                        $this->view->flag = 'success';
                        return;
                    }
                    //neu game choi bamh userId
                    if(isset($configGame->getUsernameInfo->playgameBy) && $configGame->getUsernameInfo->playgameBy == 'userId'){
                        if(!$channel){//neu k chon channel
                            foreach($arrChannel as $keyChannel => $valueChannel){
                            // $dataUsername[$keyChannel]
                                 $result= $getProfile->getDataUserName(array('userIdList'=>$arrUser,'channel'=>$keyChannel,'gameId'=>$gameId));
                                 if(isset($dataUsername[$keyChannel])){
                                    $dataUsername[$keyChannel]= array_merge($dataUsername[$keyChannel],$result);
                                 }else{
                                    $dataUsername[$keyChannel] = $result;
                                 }
                            }
                        }else{//neu co chon channel
                            $dataUsername[$channel] = $getProfile->getDataUserName(array('userIdList'=>$arrUser,'channel'=>$channel,'gameId'=>$gameId));
                        }
                    }else{//neu game choi bang username
                        if(!$channel){
                            foreach($arrChannel as $keyChannel => $valueChannel){
                                $dataUsername[$keyChannel][$arrUser] = $arrUser;
                            }
                        }else{
                            $dataUsername[$channel][$arrUser] = $arrUser;
                        }
                    } 
                    if(isset($configGame->statistics->role))
                   {
                       $title = $configGame->statistics->role->toArray();
                       $this->view->title=$title;
                       $this->view->dataUsername = $dataUsername;
                       $this->view->dataUser = $dataUser;
                       $this->view->dataRoleTitle=(current(current($dataUser)));
                   }
                   else
                   {
                       $this->view->mess = 'vui long kiểm tra lại setting của game !';
                       $this->view->flag = 'error';
                       $this->view->repair = 1;
                   }
                   
                   }//end else ss31
                }//end submit
            }//end kiem tra ini
            else{
                $this->view->mess = 'Chưa có setting cho game!';
                $this->view->flag = 'error';
                $this->view->repair = 1;
            }
        }
    }
}